package net.xdclass.product.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.common.JsonData;
import net.xdclass.product.model.vo.CartItemVO;
import net.xdclass.product.model.vo.CartVO;
import net.xdclass.product.request.CartItemRequest;
import net.xdclass.product.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public JsonData addCart(@RequestBody CartItemRequest cartItem){
        cartService.addCart(cartItem);
        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @PutMapping
    public JsonData clear(){
        return JsonData.buildSuccess(cartService.clear());
    }

    @ApiOperation("查看我的购物车")
    @GetMapping("myCart")
    public JsonData myCart(){
        CartVO cartVO = cartService.getMyCart();
        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("删除购物项")
    @DeleteMapping("/item/{productId}")
    public JsonData deleteItem(@ApiParam("商品id") @PathVariable("productId") Long productId){
        boolean success = cartService.deleteItem(productId);
        return JsonData.buildSuccess(success);
    }

    @ApiOperation("修改购物项")
    @PutMapping("/item")
    public JsonData changeCartItem(@RequestBody CartItemRequest cartItem){
        boolean success = cartService.updateCartItem(cartItem);
        return JsonData.buildSuccess(success);
    }

    @ApiOperation("查询商品最新价格")
    @PostMapping("/confirmAmount")
    public JsonData confirmOrderItem(@RequestBody List<Long> productIds){
        List<CartItemVO> cartItemVOS = cartService.confirmOrderItem(productIds);
        return JsonData.buildSuccess(cartItemVOS);
    }

}
